<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-18 17:14:17
 * @Last Modified time: 2023-02-23 16:13:37
 */
namespace app\sys\controller\store;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\store\OrderRefundService;
/**
 * 商城-售后单
 * Class OrderRefund 
 * @package app\sys\controller\store
 */
class OrderRefund extends Base
{
    /**
     * OrderRefund constructor.
     * @param App $app
     */
    public function __construct(App $app,OrderRefundService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 售后单详细
     * @noAuth(true)
     * @method(GET)
     */
    public function detail()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $this->success($this->service->getOrderRefundDetail(compact('id')));
    }

    /**
     * 售后单列表
     * @method(GET)
     */
    public function list()
    {
        $where = $this->request->getMore([
        ]);
        $this->success($this->service->getOrderRefundList($where));
    }

    /**
     * 售后单审核
     * @method(PUT)
     */
    public function audit(){
        $data = $this->request->postMore([
            'id',
            'address_id',
            'is_agree',
            'refund_money',
            'refuse_desc',
        ]);
        if (empty($data['is_agree']) || is_null($data['is_agree'])) $this->error('请选择审核状态');
        if ($this->service->audit($data)) {
            $this->success('处理成功!');
        } 
        $this->error($this->service->getError()?:'处理失败!');
    }

    /**
     * 售后单确定收货 (用户寄回)
     * @method(PUT)
     */
    public function receipt(){
        $id = $this->request->param('id');
        if ($this->service->update($id,[
            'receipt_status'=>1
        ])) {
            $this->success('处理成功');
        }
        $this->error($this->service->getError() ?: '处理失败');
    }

    /**
     * 售后单重新发货
     * @method(PUT)
     */
    public function delivery(){
        $id = $this->request->param('id');
        $data = $this->request->postMore([
            'express_id',
            'express_no',
        ]);
        $data['delivery_status'] = 1;
        $data['send_time'] = time();
        if ($this->service->update($id,$data)) {
            $this->success('处理成功');
        }
        $this->error($this->service->getError() ?: '处理失败');
    }

    /**
     * 用户已到店退回商品
     * @method(PUT)
     */
    public function arrival(){
        $id = $this->request->param('id');
        $refundMoney = $this->request->param('refund_money');
        if ($this->service->arrival($id,$refundMoney)) {
            $this->success('处理成功!');
        } 
        $this->error($this->service->getError()?:'处理失败!');
    }

    /**
     *  确定收货并执行退款
     * @method(PUT) 
     */
    public function receiptReFund(){
        $id = $this->request->param('id');
        $refundMoney = $this->request->param('refund_money');
        if ($this->service->arrival($id,$refundMoney)) {
            $this->success('处理成功!');
        } 
        $this->error($this->service->getError()?:'处理失败!');
    }
}